//
//  WKStatusRetweetView.swift
//  WKWeibo
//
//  Created by 恋guang年 on 16/4/1.
//  Copyright © 2016年 恋guang年. All rights reserved.
//

import UIKit
import SnapKit

class WKStatusRetweetView: UIView {
    //MARK：属性
    //WKStatusRetweetView底部约束
    var retweetViewBottomCon:Constraint?
    ///  被转发微博
    var retweetStatus:WKStatus? {
        didSet{
            // 当别人给被转发的模型赋值后,将模型的内容赋给对应的view
            // @发微博人的名字: 微博内容
            if let userName = retweetStatus?.user?.screen_name {
                if let content = retweetStatus?.text {
                    contentLabel.text = "@我是转发微博内容\(userName):\(content)"
                    //先卸载底部约束
                    self.retweetViewBottomCon?.uninstall()
                    if let imageURLs = retweetStatus?.storeImageURLs {
                        retweetPictures.hidden = false
                        self.snp_makeConstraints(closure: { (make) -> Void in
                            self.retweetViewBottomCon =  make.bottom.equalTo(retweetPictures.snp_bottom).offset(WKStatusCellMargin).constraint
                            retweetPictures.imageURLs = imageURLs
                        })
                    }else{
                        retweetPictures.hidden = true
                        self.snp_makeConstraints(closure: { (make) -> Void in
                            self.retweetViewBottomCon = make.bottom.equalTo(contentLabel).offset(WKStatusCellMargin).constraint
                        })
                    }
                }
                
            }
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //在initwithframe方法中添加子控件及其约束 不要在layoutsubviews中添加,会多次添加约束出现问题
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加子控件及其约束
        prepareForUI()
        backgroundColor = UIColor.randomColor()
    }
    
    ///添加子控件及其约束
    private func prepareForUI(){
        //添加子控件
        addSubview(bgButton)
        addSubview(contentLabel)
        addSubview(retweetPictures)
        //添加约束
        //按钮
        bgButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        //Label
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(WKStatusCellMargin)
            make.leading.equalTo(self).offset(WKStatusCellMargin)
            make.trailing.equalTo(self).offset(-WKStatusCellMargin)
        }
        //配图
        retweetPictures.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(WKStatusCellMargin)
            make.size.equalTo(CGSize(width: 150, height: 150))
        }
        //WKStatusRetweetView底部约束先以配图底部为标准 用来确定其高度
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottomCon = make.bottom.equalTo(retweetPictures).offset(WKStatusCellMargin).constraint
        }
    }
    
    //背景按钮
    private lazy var bgButton:UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        return button
    }()
    //显示微博内容的Label
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(WKStatusContentDefaultSize)
        label.text = "dfdasfdsfdsgfccvnkcnkjkdjfkajfdkljflsdfsdlf"
        label.numberOfLines = 0
        return label
    }()
    //配图
    private lazy var retweetPictures:WKStatusPictureView = WKStatusPictureView()
}
